package org.ainlolcat.ainscience.utils.executor.impl;

import org.ainlolcat.Variable;
import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.utils.PropertiesUtils;
import org.ainlolcat.ainscience.utils.executor.DataProvider;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ainlolcat
 *         Date: 11/4/13
 */
public class DataProviderImpl implements DataProvider {
    static Map<String, Variable> constants = new HashMap();

    public Variable getConstant(String name) {
        if (constants.containsKey(name))
            return constants.get(name);
        Variable variable = null;
        if (Controller.getInstance().getProject() != null)
            variable = Controller.getInstance().getProject().getVariable(name);

        if (variable != null){
            constants.put(name, variable);
            return variable;
        }

        String propertiesValue = PropertiesUtils.getProperty(name);
        if (propertiesValue != null){
            if (propertiesValue.matches("-?\\d+(\\.\\d+)?(E[+-]?\\d+)?")){
                Double value = Double.valueOf(propertiesValue);
                Variable var = new Variable(name, value, Variable.TYPE_DOUBLE);
                constants.put(name, var);
                return var;
            }
        }

        return new Variable(name);
    }
}
